import Vue from 'vue'
import VueRouter from 'vue-router'
import login from '../components/login.vue'
import register from '../components/register.vue'
import home from '../view/home.vue'
import chatpage from '../components/chatpage'
import revise from '../components/revise'
import addfriend from '../components/addfriend'
import agree from '../components/agree'
// import { component } from 'vue/types/umd'

Vue.use(VueRouter)

const routes = [{
  path: '/',
  redirect: '/home',

}, {
  path: '/login',
  component: login
}, {
  path: '/register',
  component: register
}, {
  path: '/home',
  component: home,
},{
  path:'/addfriend',
  component:addfriend
},
{
  path:'/agree',
  component:agree
},
    {
      path:'/chatpage',
      component:chatpage
    },
    {
      path:'/revise',
      component:revise
    }
]
const originalReplace = VueRouter.prototype.replace;
VueRouter.prototype.replace = function replace(location) {
  return originalReplace.call(this, location).catch(err => err);
};

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 主入口路由(需嵌套上左右整体布局)
router.beforeEach((to, from, next) => {
  if (to.path === '/login') {
    next()
  } else if(to.path === '/register'){
    next()
  } else{
    let token = localStorage.getItem('token')
    let time = localStorage.getItem('nowtime')
    var newtime = Date.parse(time)
    var currentTime = Date.parse(new Date()); //获取当前时间戳
    if (!token || (currentTime - newtime) >1000 *60*300 ){
      localStorage.removeItem('token')
      localStorage.removeItem('nowtime')
      next('/login')
    }
    next()
  }
})


export default router
